function iniciate_diagram_labels(image, points) {
    image = $(image);
    image.mousemove(function(event) {
        var position = $(this).offset();
        var x = Math.floor(event.pageX - position.left);
        var y = Math.floor(event.pageY - position.top);
        var r = 7;
        var text = '';
        for(i in points) {
            point = points[i];
            if (point.x - r < x && x < point.x + r && point.y - r < y && y < point.y + r) {
                text = text + '<b>' + point.y_axis + '</b> ' + get_lagend_label(point.legend_id) + ' (<i>' + point.x_axis + '</i>) <br/>';
            }
        }
        if (text) {
            show_tooltip(event.pageX + 20, event.pageY, text);
        } else {
            hide_tooltip();
        }
    });
    image.mouseleave(function() {
        hide_tooltip();
    });
}

function get_lagend_label(id) {
    var label = $("." + id);
    if (label.length > 1) {
        label = $(label[0]);
    } else if (label.length == 0) {
        return '';
    }
    return "<span style='" + label.attr('style') + "'>" + label.text() + '</span>';
}

var tooltip = null;
function show_tooltip(x, y, html) {
    if (!tooltip) {
        tooltip = $("#tooltip");
        if (tooltip.length == 0) {
            tooltip = $("<span/>");
            tooltip.attr('id', 'tooltip');
            $("body").append(tooltip);
        }
    }
    tooltip.html(html)
    if (x + tooltip.width() + 30 > $('body').width()) {
        x -= tooltip.width() + 30;
    }
    tooltip.css('left', Math.floor(x) + 'px');
    tooltip.css('top', Math.floor(y) + 'px');
    tooltip.css('display', 'block');
}

function hide_tooltip() {
    var tooltip = $("#tooltip");
    if (tooltip) {
        tooltip.css('display', 'none');
    }
}

function add_manual_tooltip(object) {
    object.new_title = object.title;
    object.onmousemove = function(event) {
        if (!event) {
            event = window.event;
        }
        x = event.pageX;
        y = event.pageY;
        if(x == undefined){
            x = event.clientX + document.body.scrollLeft
            y = event.clientY + document.body.scrollTop
        }
        show_tooltip(x, y+20, this.new_title);
        this.title = '';
        var validType = this instanceof HTMLTableCellElement || HTMLSpanElement;
        if (validType) {
            var hover_set = /\s*hover\s*/.test(this.className);
            if (!hover_set) {
                this.className = this.className + " hover";
            }
        }
    };
    object.onmouseout = function(event) {
        hide_tooltip();
        var validType = this instanceof HTMLTableCellElement || HTMLSpanElement;
        if (validType) {
            this.className = this.className.replace(/\s*hover\s*/, '');
        }
    }
}

function replace_tooltips() {
    tooltips = $('[title]');
    for(var i=0; i<tooltips.length; i++) {
        add_manual_tooltip(tooltips[i]);
    }
}

$(document).ready(function() {
    replace_tooltips();
});